package com.example.administrator.testc.News;/**
 * Created by Administrator on 2016/3/11.
 */

import android.content.Context;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.administrator.testc.R;

import java.util.List;

import pri.zey.custom.tools.image.RealizeAdapter;
import pri.zey.custom.tools.image.TImageLoaderThread;
import pri.zey.custom.tools.image.ViewHolder;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class AdapterNews2 extends RealizeAdapter<NewsBean> implements AbsListView.OnScrollListener{

    private List<NewsBean> mList;
//    private TImageLoaderAsyncTask mImageLoader;
    private TImageLoaderThread mImageLoader;
    private int start, end;
    private boolean isFirstIn = true;

    public AdapterNews2(Context mContext, List<NewsBean> mList, ListView listView) {
        super(mList, mContext, R.layout.z_row_news);
        this.mList = mList;
//        this.mImageLoader = new TImageLoaderAsyncTask(listView, mList);
        this.mImageLoader = new TImageLoaderThread(listView, mList);
        listView.setOnScrollListener(this);
    }
    //滚动过程中变动触发
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if(scrollState == SCROLL_STATE_IDLE){
            //加载可见项
            mImageLoader.loadImages(start, end);
        }else{
            //停止任务
            mImageLoader.cancelAllTask();
        }
    }

    //滑动过程中触发（包括第一次加载）
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.start = firstVisibleItem;
        this.end = firstVisibleItem + visibleItemCount;
        //第一次显示时候使用
        //visibileItemCount > 0条件为了跳过一开始多次加载
        if(isFirstIn && visibleItemCount > 0){
            mImageLoader.loadImages(start, end);
            isFirstIn = false;
        }
    }

    @Override
    public void setConvert(int position, ViewHolder holder) {
        ImageView imageView = holder.getView(R.id.imageView1);
        imageView.setImageResource(R.mipmap.ic_launcher);
        imageView.setTag(mList.get(position).getmPhotoURL());
        holder.setText(R.id.titleView, mList.get(position).getmTitle());
        holder.setText(R.id.contentView, mList.get(position).getmContent());
    }
}
